package net.phoenix.repository.nodetype;

import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;

import net.phoenix.repository.data.PropertyDefinitionData;

/**
 * 
 * @author lixf
 * 
 */
public class XMLPropertyDefinition extends GeneralPropertyDefinition {
	//private static final Log log = LogFactory.getLog(XMLPropertyDefinition.class);

	private PropertyDefinitionData data;
	private Value[] defaultValues;

	public XMLPropertyDefinition(XMLNodeTypeDefinitionManager manager, XMLNodeType parent, PropertyDefinitionData data) {
		super(manager, parent);
		this.data = data;
//		if (data.getDefaultValues() != null) {
//			this.defaultValues = new Value[data.getDefaultValues().length];
//			for (int i = 0; i < this.defaultValues.length; i++) {
//				try {
//					this.defaultValues[i] = manager.getValueFactory().createValue(data.getDefaultValues()[i], this.data.getType());
//				} catch (ValueFormatException e) {
//					log.warn("Error to set default value for " + data.getDefaultValues()[i] + " in" + this.data + ".");
//				}
//			}
//		} else
			this.defaultValues = new Value[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.PropertyDefinition#getName()
	 */
	@Override
	public String getName() {
		return this.data.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.PropertyDefinition#getDefaultValues()
	 */
	@Override
	public Value[] getDefaultValues() {
		return this.defaultValues;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.PropertyDefinition#getDefaultValue()
	 */
	@Override
	public Value getDefaultValue() {
		if (this.defaultValues.length == 0)
			return null;
		return this.defaultValues[0];
	}

	@Override
	public int getRequiredType() {
		return this.data.getType();
	}

	@Override
	public String[] getValueConstraints() {
		return null;
	}

	@Override
	public boolean isMultiple() {
		return this.data.isMultiple();
	}

	@Override
	public String[] getAvailableQueryOperators() {
		return null;
	}

	@Override
	public boolean isFullTextSearchable() {
		return this.data.isSearchable();
	}

	@Override
	public boolean isQueryOrderable() {
		return this.data.isOrderable();
	}

	@Override
	public NodeType getDeclaringNodeType() {
		return this.nodeType;
	}

	@Override
	public boolean isAutoCreated() {
		return this.data.isAutoCreated();
	}

	@Override
	public boolean isMandatory() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getOnParentVersion() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isProtected() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public int getMinLength() {
		return this.data.getMinLength();
	}
	
	public int getMaxLength() {
		return this.data.getMaxLength();
	}

}
